<template>
  <div class="app-container">
    <div class="module-title">
      <svg-icon icon-class="shopping" style="font-size:28px;margin-right:8px;"/>
      <span>退料管理</span>
      <span class="module-desc">管理生产退料明细</span>
    </div>
    <div class="module-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            v-model="queryParams.materialName"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="goAddPage"
            v-hasPermi="['returnMaterials:returnMaterials:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['returnMaterials:returnMaterials:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['returnMaterials:returnMaterials:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['returnMaterials:returnMaterials:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="returnMaterialsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键ID" align="center" prop="id" />
        <el-table-column label="单据类型" align="center" prop="documentType" />
        <el-table-column label="来源单据" align="center" prop="sourceDocument" />
        <el-table-column label="退料单号" align="center" prop="returnCode" />
        <el-table-column label="规格" align="center" prop="specification" />
        <el-table-column label="物料编码" align="center" prop="materialCode" />
        <el-table-column label="物料名称" align="center" prop="materialName" />
        <el-table-column label="实退数量" align="center" prop="actualReturnQty">
          <template slot-scope="scope">
            <span class="quantity-amount">{{ scope.row.actualReturnQty || '0' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="核销数量" align="center" prop="writeOffQty">
          <template slot-scope="scope">
            <span class="quantity-amount">{{ scope.row.writeOffQty || '0' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="auditStatus">
          <template slot-scope="scope">
            <el-tag :type="getAuditStatusType(scope.row.auditStatus)">
              {{ scope.row.auditStatus }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="退料日期" align="center" prop="returnDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退料人" align="center" prop="returnBy" />
        <el-table-column label="创建人" align="center" prop="createdBy" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['returnMaterials:returnMaterials:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['returnMaterials:returnMaterials:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改生产退料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="单据类型" prop="documentType">
              <el-select v-model="form.documentType" placeholder="请选择单据类型">
                <el-option label="生产退料" value="生产退料"></el-option>
                <el-option label="质量退料" value="质量退料"></el-option>
                <el-option label="计划变更退料" value="计划变更退料"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联工单" prop="workOrderId">
              <el-input v-model="form.workOrderId" placeholder="请输入关联工单号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="关联领料单" prop="pickingId">
              <el-input v-model="form.pickingId" placeholder="请输入关联领料单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="来源单据" prop="sourceDocument">
              <el-input v-model="form.sourceDocument" placeholder="请输入来源单据" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退料单号" prop="returnCode">
              <el-input v-model="form.returnCode" placeholder="请输入退料单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格" prop="specification">
              <el-input v-model="form.specification" placeholder="请输入规格" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="实退数量" prop="actualReturnQty">
              <el-input v-model="form.actualReturnQty" placeholder="请输入实退数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核销数量" prop="writeOffQty">
              <el-input v-model="form.writeOffQty" placeholder="请输入核销数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退料日期" prop="returnDate">
              <el-date-picker clearable
                v-model="form.returnDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择退料日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审核状态" prop="auditStatus">
              <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
                <el-option label="待审核" value="待审核"></el-option>
                <el-option label="已审核" value="已审核"></el-option>
                <el-option label="已拒绝" value="已拒绝"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="退料人" prop="returnBy">
              <el-input v-model="form.returnBy" placeholder="请输入退料人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建人" prop="createdBy">
              <el-input v-model="form.createdBy" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReturnMaterials, getReturnMaterials, delReturnMaterials, addReturnMaterials, updateReturnMaterials } from "@/api/returnMaterials/returnMaterials"

export default {
  activated(){
      this.getList()
  },
  name: "ReturnMaterials",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产退料表格数据
      returnMaterialsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询生产退料列表 */
    getList() {
      this.loading = true
      listReturnMaterials(this.queryParams).then(response => {
        this.returnMaterialsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        documentType: null,
        sourceDocument: null,
        returnCode: null,
        specification: null,
        materialCode: null,
        materialName: null,
        actualReturnQty: null,
        writeOffQty: null,
        auditStatus: null,
        returnDate: null,
        returnBy: null,
        createdBy: null,
        createdAt: null,
        updatedAt: null,
        remark: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加生产退料"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getReturnMaterials(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改生产退料"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReturnMaterials(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addReturnMaterials(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除生产退料编号为"' + ids + '"的数据项？').then(function() {
        return delReturnMaterials(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('returnMaterials/returnMaterials/export', {
        ...this.queryParams
      }, `returnMaterials_${new Date().getTime()}.xlsx`)
    },
    goAddPage() {
      this.$router.push({ path: '/returnMaterials/returnMaterials/add' });
    },
    /** 获取审核状态标签类型 */
    getAuditStatusType(status) {
      switch(status) {
        case '待审核':
          return 'warning'
        case '已审核':
          return 'success'
        case '已拒绝':
          return 'danger'
        default:
          return 'info'
      }
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/module-card.scss";

.module-title {
  display: flex;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: #2980b9;
  margin-bottom: 18px;
  .module-desc {
    font-size: 14px;
    color: #888;
    font-weight: 400;
    margin-left: 18px;
  }
}

.quantity-amount {
  color: #2980b9;
  font-weight: 600;
}

.el-table {
  .el-table__body tr:hover > td {
    background: #eaf6ff !important;
  }
}

.el-dialog {
  .el-form-item {
    margin-bottom: 20px;
  }
}
</style>
